import { Ref, ref, watchEffect } from 'vue'
import { NumData } from '../types'

/**
 * 心理安全预警
 */
export function usePsychologicalSafetyWarning({
  numData,
}: {
  numData: Ref<NumData | null>
}) {
  const seriesData = ref<{ value: number; name: string; code: string }[]>([])

  watchEffect(() => {
    if (!Array.isArray(numData.value?.levelNum)) {
      return
    }

    if (!Array.isArray(numData.value?.allTestNum)) {
      return
    }

    if (!Array.isArray(numData.value?.levelData)) {
      return
    }

    const levelData = numData.value?.levelData || []

    const levelDictValueData = []
    for (let zz = 0; zz < levelData.length || 0; zz++) {
      levelDictValueData.push(levelData[zz].dictValue)
    }

    seriesData.value = []

    const testNum = numData.value?.findReleaseFinishTestNum[0]?.num || 0

    const nameList = ['正常人数'].concat(levelDictValueData)
    let allNum = 0
    numData.value?.levelNum.map((item) => {
      if (!item || !item.code) {
        return false
      }
      allNum += item.num
      const index = Number(item.code)
      seriesData.value.push({
        value: item.num,
        name: nameList[index],
        code: item.code,
      })
    })
    seriesData.value.unshift({
      value: testNum - allNum,
      // value: 527,
      name: '正常人数',
      code: '0',
    })

    // console.log(seriesData)
  })
  return {
    seriesData,
  }
}
